/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package agents;

import env.Action;
import env.Enviroment;
import env.Orientation;
import java.awt.Color;
import javax.swing.Icon;
import javax.swing.ImageIcon;

/**
 *
 * @author orcas
 */
public class AutomatonAgent extends Agent {
    static Icon iconNorth=new ImageIcon("images/AgentNorthRed.GIF");
    static Icon iconSouth=new ImageIcon("images/AgentSouthRed.GIF");
    static Icon iconEast=new ImageIcon("images/AgentEastRed.GIF");
    static Icon iconWest=new ImageIcon("images/AgentWestRed.GIF");
    private int row,col;

    private enum STATE {
        GOING_LEFT, GOING_RIGHT, TURNING, ERROR;  //; is optional
    }
    private STATE state;

    public AutomatonAgent(String name, int row, int col, Orientation orientation, Color color) {
        super( name, row, col, orientation, color);
        state = STATE.GOING_LEFT;
        super.setArmed(true);
    }
    
    public AutomatonAgent(AutomatonAgent e) {
        super( e.getName(), e.getRow(), e.getCol(), e.getOrientation(), e.getColor());
        state = e.state;
    }
    

    public Action getMove() {
        Action move = null;
        //System.out.print("STATE = "+state+", ");
        switch (state) {
            case GOING_LEFT:
                //if (getEnv().isCellFree(getRow(), getCol() - 1)) {
                if (!getEnv().map[getRow()][getCol() - 1].isWall()) {
                    move = Action.fwd;
                } else {
                    state = STATE.TURNING;
                    move = Action.right;
                }
                break;
            case GOING_RIGHT:
                //if (getEnv().isCellFree(getRow(), getCol() + 1)) {
                if (!getEnv().map[getRow()][getCol() + 1].isWall()) {
                    move = Action.fwd;
                } else {
                    state = STATE.TURNING;
                    move = Action.right;
                }
                break;
            case TURNING: {
                Orientation orientation = getOrientation();
                if (orientation.equals(Orientation.North) || orientation.equals(Orientation.South)) {
                    move = Action.right; //keep turning!
                } else { //start going
                    if (orientation.equals(Orientation.West)) {
                        state = STATE.GOING_LEFT;
                    } else if (getOrientation().equals(Orientation.East)) {
                        state = STATE.GOING_RIGHT;
                    }
                    else state=STATE.ERROR;
                    move = Action.fwd;
                }
                break;
            }
        }
        //System.out.println(getName()+"'s move is "+move.toString());
        return move;
    }

    @Override
    public Icon getIcon() {
        switch(this.getOrientation()) {
            case North: return iconNorth;
            case East: return iconEast;
            case West: return iconWest;
            case South: return iconSouth;
        }
        return null;
    }

    @Override
    public void moveFailed() {
        this.state=STATE.ERROR;
    }


    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final AutomatonAgent other = (AutomatonAgent) obj;
//        if (this.getRow() != other.getRow()) {
//            return false;
//        }
        if (this.getCol() != other.getCol()) {
            return false;
        }
        if (this.state != other.state) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        //hash = 97 * hash + this.getRow();
        hash = 97 * hash + this.getCol();
        hash = 97 * hash + (this.state != null ? this.state.hashCode() : 0);
        return hash;
    }
    
    
    
}
